package com.atguigu.com.exam;

/**
 * @author xbd
 * @create 2021-05-23 12:07
 */

class Clear {

    private int productCount = 0;
    private int bread = 0;

    public void make() {
        if (productCount < 20) {
            productCount++;
            System.out.println(Thread.currentThread().getName() + "开始生产产品：" + productCount);
        }else if(productCount % 20 == 0){
                eat();
        }else if(productCount < 50){
            productCount++;
            System.out.println(Thread.currentThread().getName() + "开始生产产品：" + productCount);
        }else{
            System.out.println("生产完成。。。");
            return;
        }
    }

    public void eat(){
        if(bread < 3){
            bread++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "开始吃馒头：" +bread);
        }else{
            bread = 0;
            productCount++;
            System.out.println(Thread.currentThread().getName() + "开始生产产品：" + productCount);
        }
    }
}

class producter implements Runnable {

    private Clear clear;

    public producter(Clear clear) {
        this.clear = clear;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "开始生产产品。。。。");

        while (true) {

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            clear.make();
        }
    }
}

public class ProductPlay {
    public static void main(String[] args) {
        Clear clear = new Clear();
        producter p1 = new producter(clear);
        Thread t1 = new Thread(p1);
        t1.start();
    }
}
